package org.example.oop.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * org.example.oop.stream
 *
 * @author moLei
 * @description
 * @since 2025/6/21
 */
public class StreamDemo01 {


    public static void main(String[] args) {

        Map<String, String> map = new HashMap<>();
        map.put("1", "hello");
        map.put("2", "world");
        map.put("3", "java");
        map.put("4", "stream");

        // stream的中间方法是返回一个新的stream流对象，原来的stream只能使用一次
        map.keySet().stream().filter(key ->  map.get(key).equals("java")).forEach(key -> System.out.println(map.get(key)));

        // of中需要放引用类型的数组，不能用基础类型，不然会将其识别为一个对象
        int[] arr = {1, 2, 3, 4, 5};
        Stream.of(arr).forEach(System.out::println);

        Integer[] arr1 = {1, 2, 3, 4, 5};
        Stream.of(arr1).forEach(System.out::println);

        // stream中的distinct去重方法使用的是HashSet集合进行去重
        Stream.of(1, 2, 3, 4, 5, 1, 2, 3, 4, 5).distinct().forEach(System.out::println);

        // 转换为数组
        Integer[] arr2 = Stream.of(1, 2, 3, 4, 5).toArray(Integer[]::new);
        Stream.of(arr2).toArray(value -> new Integer[value]);

        // 集合转map
        List<String> list = Arrays.asList("moeli-man", "mo-women");
        Map<String, String> collect = list.stream().collect(Collectors.toMap(s -> s.split("-")[0], s -> s.split("-")[1]));
        for (String s : collect.keySet()) {
            System.out.println(s + ":" + collect.get(s));
        }

        List<String> list1 = Arrays.asList("张三,23", "李四,24", "王五,25");
        Map<String, Integer> collect1 = list1.stream()
                .filter(s -> Integer.parseInt(s.split(",")[1]) >= 24)
                .collect(Collectors.toMap(s -> s.split(",")[0], s -> Integer.parseInt(s.split(",")[1])));
        for (String s : collect1.keySet()) {
            System.out.println(s + ":" + collect1.get(s));
        }

        List<String> manList = Arrays.asList("张三,23", "王五,25", "古天乐,27", "周杰伦,28", "张学友,29", "周杰伦,28");
        List<String> womenList = Arrays.asList("杨四,24", "赵六,26", "刘亦菲,30", "佐山爱,32", "王菲,31", "杨幂,34");
        Map<String, String> collect2 = Stream.concat(
                manList.stream()
                        .filter(s -> s.split(",")[0].length() == 3)
                        .limit(2),
                womenList.stream()
                        .filter(s -> s.split(",")[0].startsWith("杨"))
                        .skip(1)
        ).collect(Collectors.toMap(s -> s.split(",")[0], s -> s.split(",")[1]));

        List<Actor> actors = new ArrayList<>();
        collect2.keySet().forEach(key -> {
            Actor actor = new Actor();
            actor.setName(key);
            actor.setAge(collect2.get(key));
            actors.add(actor);
        });

        System.out.println("---");
        actors.forEach(actor -> System.out.println(actor.getName() + ":" + actor.getAge()));



    }

    static class Actor {
        private String name;

        private String age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }
    }
}
